﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using LUC.Domains;
using System.Drawing;
using System.Xml.Linq;
using System.Windows.Forms;
using LUC.Properties;

namespace LUC
{
	class Controller
	{
		public static XmlTextReader InsertConnStr(string xml_cfg, string connection_str)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load(xml_cfg);
			foreach (XmlNode t_node in doc.DocumentElement.ChildNodes[0].ChildNodes)
			{
				if (t_node.Attributes["name"].Value == "connection.connection_string")
				{
					t_node.ChildNodes[0].Value = connection_str;
					break;
				}
			}
			doc.Save(xml_cfg);
			return new XmlTextReader(xml_cfg);
		}

		#region Get Text for labels
		static string s_time_over = "Time is over!";
		static string[] lefts = { "Left", "Left", "Left" };
		static string[] passed = { "Passed", "Passed", "Passed" };
		static string[] days = { "day", "days", "days" };
		static string[] hours = { "hour", "hours", "hours" };
		static string[] minutes = { "minute", "minutes", "minutes" };

		
		// Return suffix by digit
		// 0 - прошел	остался		час	минута	день
		// 1 - прошло	осталось		часа	минуты	дня
		// 2 - прошло	осталось		часов	минут		дней
		static int GetSuffix(int x)
		{
			// x = {n2 n1}
			int n1 = Math.Abs(x) % 10;
			int n2 = (Math.Abs(x) / 10) % 10;

			if ((n2 != 1) && (n1 == 1))
				return 0; // 1, 21, 101 ...
			if (n2 == 1)
				return 2; // 11 12 13 14 15 16 17 18 19 
			if ((n1 == 2) || (n1 == 3) || (n1 == 4))
				return 1;
			return 2;
		}
		public static string GetText(DateTime dt, ref bool hm)
		{
			hm = false;
			StringBuilder sb = new StringBuilder();
			int dif = Convert.ToInt32((dt - DateTime.Now).TotalDays);
			if (dif == 0)
			{
				if ((dt.TimeOfDay < DateTime.Now.TimeOfDay) || (dt.TimeOfDay.TotalSeconds == 0))
					sb.Append(s_time_over);
				else
				{
					TimeSpan ts = (dt - DateTime.Now);
					sb.Append(lefts[GetSuffix(ts.Hours)]).Append(" ").
						Append(ts.Hours).Append(" ").Append(hours[GetSuffix(ts.Hours)]).Append(" ").
						Append(ts.Minutes).Append(" ").Append(minutes[GetSuffix(ts.Minutes)]);
					hm = true;
				}
			}
			else
			{
				if (dif > 0)
					sb.Append(lefts[GetSuffix(dif)]);
				else
					sb.Append(passed[GetSuffix(dif)]);
				dif = Math.Abs(dif);
				sb.Append(" ").Append(dif).Append(" ").Append(days[GetSuffix(dif)]);
			}
			return sb.ToString();
		}
		#endregion
	
	}
}
